package com.example.administrator.shzs.ui;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.administrator.shzs.R;
import com.example.administrator.shzs.activity.BaseActivity;
import com.example.administrator.shzs.app.MyApplication;
import com.example.administrator.shzs.url.Contents;
import com.google.gson.Gson;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import butterknife.Bind;
import butterknife.ButterKnife;
/**
 * 兑换商品成功 填写地址页面
 */
public class IntegraEditActivity extends BaseActivity implements View.OnClickListener {


    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.Title)
    TextView Title;
    @Bind(R.id.Integral)
    TextView Integral;
    @Bind(R.id.Linkman)
    EditText Linkman;
    @Bind(R.id.TelPhone)
    EditText TelPhone;
    @Bind(R.id.Address)
    EditText Address;
    @Bind(R.id.button)
    Button button;
    @Bind(R.id.activity_integra_edit)
    LinearLayout activityIntegraEdit;

    private String title;
    private String Intergra;

    private Context context = this;
    //    当前时间
    private String str;

    //    用户id
    private String userid;
    //商品id
    private String GiftsId;
    //
    private String phonee;
    //手机
    private String address;
    private String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_integra_edit);
        ButterKnife.bind(this);
        MyApplication.app.activityList.add(this);

        title = getIntent().getStringExtra("title");
        Intergra = getIntent().getStringExtra("Intergra");
        GiftsId = getIntent().getStringExtra("GiftsId");

        back.setOnClickListener(this);

        //        获取当前时间
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date data = new Date(System.currentTimeMillis());
        str = formatter.format(data);
        //商品名字
        Title.setText(title);
        //所需积分
        Integral.setText(Intergra);
        //        用户名字
        name = MyApplication.app.getUsername();

        //        用户手机号
        phonee = MyApplication.app.getPhone();


//        用户id
        userid = MyApplication.app.getUserId();
        button.setOnClickListener(this);

    }


    private void initDataFromWeb() {

        RequestParams urll = new RequestParams(Contents.SUBMIT);
        String h = "[";
        String f = "]";

        LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
        map.put("Address", Address.getText().toString().trim());
        map.put("Cdate", str);
        map.put("CustId", userid);
        map.put("Date", "");
        map.put("DetailId", "0");
        map.put("GiftsId", GiftsId);
        map.put("PayIntegral", Intergra);
        map.put("Person", Linkman.getText().toString().trim());
        map.put("Phone", TelPhone.getText().toString().trim());
        map.put("State", "0");
        map.put("GiftName", title);
        map.put("UserName", MyApplication.app.getUsername());
        map.put("remark", "");
        Gson gson = new Gson();
        String json = gson.toJson(map);

        String params = h + json + f;
        urll.addBodyParameter("ordermodel", params);

        x.http().post(urll, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    int s = jsonObject.getInt("state");

                    if (s == 1) {
                        Toast.makeText(IntegraEditActivity.this, "兑换成功", Toast.LENGTH_SHORT).show();

                    } else {
                        Toast.makeText(IntegraEditActivity.this, "兑换失败", Toast.LENGTH_SHORT).show();
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });


    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.button:
                //手机
                address = Linkman.getText().toString().trim();
                phonee = TelPhone.getText().toString().trim();
                name = Address.getText().toString().trim();
                if (address.equals("")  || phonee.equals("") || name.equals("")) {
                    Toast.makeText(context, "信息填写不完整", Toast.LENGTH_SHORT).show();

                } else {
                    Pattern p = Pattern.compile("^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[06-8])\\d{8}$");
                    //判断用户的手机号码格式是否正确
                    Matcher m = p.matcher(phonee);
                    boolean result = m.matches();
                    if (result) {
                        initDataFromWeb();
                    } else {
                        Toast.makeText(context, "请输入正确手机号码", Toast.LENGTH_SHORT).show();
                    }
                }

                break;
            case R.id.back:
                onBackPressed();
                break;
        }
    }
}
